package filter

import (
	"github.com/beego/beego/v2/server/web/context"
	R "ksd-social-api/commons/result"
)

func AppidFilter(ctx *context.Context) {
	isUrlExist := ValidateURL(ctx.Request.URL.Path)
	if !isUrlExist {
		// 1: 获取头部信息的appid
		AppidArr := ctx.Request.Header["Appid"]
		SystemIdArr := ctx.Request.Header["Systemid"]
		if len(AppidArr) == 0 || len(AppidArr[0]) == 0 {
			ctx.Output.JSON(R.FailCodeMsg(60001, "不受信任的网站，非法请求"), true, false)
			return
		}

		// 2: 开始判断appid是不是在白名单中
		appid := AppidArr[0]
		systemId := SystemIdArr[0]
		exsit := ValidateAppid(appid)
		if !exsit {
			ctx.Output.JSON(R.FailCodeMsg(60001, "appid错误，非法请求"), true, false)
			return
		}

		ctx.Input.SetData("systemId", systemId)
	}
}
